package com.antu.first.web.frontend;

import com.alibaba.fastjson.JSON;
import com.antu.first.bean.DiscountCoupon;
import com.antu.first.bean.User;
import com.antu.first.dto.DiscountCouponExecution;
import com.antu.first.exceptions.DiscountCouponException;
import com.antu.first.service.DiscountCouponService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/13.
 */
@Controller
@RequestMapping(value = "/discountCouponController",produces = "text/plain;charset=utf-8")
public class MobileDiscountCouponController {
    @Autowired
    private DiscountCouponService discountCouponService;
    Map<String,Object> modelMap=new HashMap<String,Object>();

    /**
     * 用户查询与自己相关的优惠券(进入优惠券界按状态值请求面请求，不传参数为查询所有，传参数为优惠券状态)
     * @return
     */
    @RequestMapping(value = "/usersGetAlTheCoupons",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> usersGetAlTheCoupons(User user,Integer status){
        return discountCouponService.usersGetAlTheCoupons(user,status);
    }

    /**
     * 用户到酒店详情页看，抢优惠券
     * @param discountCoupon
     * @return
     */
    @RequestMapping(value = "/userRobStamps",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object>userRobStamps(DiscountCoupon discountCoupon, HttpServletRequest httpServletRequest){
        User user= (User) httpServletRequest.getSession().getAttribute("user");
        discountCoupon.setUser(user);
        return discountCouponService.userRobStamps(discountCoupon);
    }

    /**
     * 根据条件获取用户的优惠券!其中，pageIndex必填，canUse必填，
     * canUse的值可以为0和1,0表示选择当前用户可使用的优惠券，1表示当前不可使用的
     * @param pageIndex
     * @param canUse
     * @param request
     * @return
     */
    @RequestMapping("/selectAllsByUid")
    @ResponseBody
    public String selectAllsByUid(@Param("pageIndex")int pageIndex,@Param("canUse")int canUse, HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try{
            User user = (User) request.getSession().getAttribute("user");
            if(user != null){
                DiscountCouponExecution execution = discountCouponService.queryAllsByUid(user.getuId(), canUse, pageIndex);
                if(execution.getState() != 0){
                    modelMap.put("success", false);
                    modelMap.put("errMsg", execution.getStateInfo());
                    return JSON.toJSONString(modelMap);
                }
                modelMap.put("success", true);
                modelMap.put("discountCoupons", execution.getDiscountCoupons());
                modelMap.put("recordsNum", execution.getCount());
                modelMap.put("pageNum", (execution.getCount()/10)+1);
            }else{
                modelMap.put("success", false);
                modelMap.put("errMsg", "您当前处于未登录状态，请登录！");
                modelMap.put("state", "UnLogin");
                return JSON.toJSONString(modelMap);
            }
        }catch(Exception e){
            throw new DiscountCouponException("获取用户优惠券信息的过程中发生错误，错误信息："+e.toString());
        }
        return JSON.toJSONString(modelMap);
    }
}
